var app = getApp();

Page({
  data: {
    hasTele: true,
    phone: 0
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  //获取用户信息按钮的点击事件
  getUserInfoFun: function (e) {
    var that = this;
    wx.login({ 
      success: function (res) {
        console.log(res);
        if (res.code) {
          var jscode = res.code;
          var userInfo = e.detail.userInfo;
          var p_id = wx.getStorageSync("p_id");
          console.log(userInfo);
          wx.setStorageSync("userInfo", userInfo);
          app.getRequest(
            "api/login/getOpenid", {
              js_code: jscode
            },
            function (res_user) {
              var userId = res_user.openid;
              wx.setStorageSync("openId", userId);
                userInfo.openid = userId;
                var phone = wx.getStorageSync("user").phone;
                if (phone === "") {
                  that.setData({
                    status: true
                  })
                } else{
                  phone = phone ? phone : "";
                  userInfo.phone = phone;
                  var params = {
                    openid: userInfo.openid,
                    nickName: userInfo.nickName,
                    avatarUrl: userInfo.avatarUrl,
                    gender: userInfo.gender,
                    phone: userInfo.phone
                  }
                  if (p_id){
                    params.p_id = p_id;
                  }
                  app.getRequest("api/login/userlogin", params, function (rres) {
                    if (rres.code == 200) {
                      wx.setStorageSync("u_id", rres.data.u_id);
                      wx.setStorageSync("user", rres.data);
                      wx.redirectTo({
                        url: '/pages/firstPage/first',
                      })                    
                      // that.setData({
                      //   status: true
                      // })
                    } else {
                      wx.showToast({
                        title: rres.message,
                        icon: "none"
                      })
                    }
                  });
                }
            });
        }

      }
    });
  },  
  //登录
  login: function () {
    // this.setData({
    //   status: true
    // })
  },
  //获取手机号
  getPhoneNumber: function (e) {
    console.log(e)
    //获取手机号，保存至缓存
    var phone = e.detail.iv;
    console.log(phone)
    wx.showModal({
      title: '',
      content: phone,
    })
    phone = phone ? phone : "";
    wx.setStorageSync("phone", phone);
    //再缓存中获取openid和userInfo
    var userInfo = wx.getStorageSync("userInfo");
    var openId = wx.getStorageSync("openId");
    var params = {
      openid: openId,
      nickName: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl,
      gender: userInfo.gender,
      phone: phone
    }
    // 正常的appid，使用这段代码
    if (e.detail.errMsg === "getPhoneNumber:ok"){
      // app.getRequest("api/login/userlogin", params, function (
      //   rres
      // ) {
      //   if (rres.code == 200) {
      //     wx.setStorageSync("u_id", rres.data.u_id);
      //     wx.setStorageSync("user", rres.data);
      //   }
      // });
      wx.redirectTo({
        url: '/pages/firstPage/first',
      })                   
    }
  }
})

function addHttp(len, arr) {
  var imgU = [];
  for (var i = 0; i < len; i++) {
    var item = arr[i].pic;
    item = app.globalData.globalUrl + item;
    imgU.push(item);
  }
  return imgU;
}